#Include "../common/log.ahk"
#Include "../common/bt.ahk"
#SingleInstance Force
SendMode "Input"
SetKeyDelay 0

Init()

esIni := FileExist(iniPath)
if(esIni) {
    ConfigRead()
} else
{
    InIWriter()
}
myGui()

F1:: {
    MouseGetPos &MouseX, &MouseY
    color := PixelGetColor(MouseX, MouseY)
    obj.color:=color
    obj.x:=MouseX
    obj.y:=MouseY
    InIWriter()
}

;初始化
Init() {
    global iniPath := "./ls.ini"
    map := 
    
    global obj := {
        rKey:52,
        tKey:23,
        color:"",
        x:0,
        y:0,
        keyModel:0
    }
}

;写入配置文件
InIWriter() {

    WriterLog("写入配置")
}

;读取配置文件
ConfigRead() {
    obj.rKey := IniRead(iniPath, "ls", "rKey")
    obj.tKey := IniRead(iniPath, "ls", "tKey")
    obj.color := IniRead(iniPath, "ls", "color")
    obj.x := IniRead(iniPath, "ls", "x")
    obj.y := IniRead(iniPath, "ls", "y")
    obj.keyModel := IniRead(iniPath, "ls", "keyModel", 0)
    if(obj.keyModel=0) {
        global codeKey:="XButton2"
    } else if(obj.keyModel=1) {
        global codeKey:="XButton1"
    } else {
        global codeKey:="XButton2"
    }
    WriterLog("读取到配置")
}

; 写入日志
WriterLog(name) {
    str:=name "：rKey:" obj.rKey " tKey:" obj.tKey " x:" obj.x " y:" obj.y `n" color:" obj.color " keyModel:" obj.keyModel
    Log.log(&str)
}

myGui() {
    MyGui := Gui()
    MyGui.Add("Text", "ym+7 xm+0 Section", "如果选择 侧键1 则 侧键2会触发ss，更精细的调整期待后续版本`n ")

    MyGui.Add("Text", "ym+27 xm+0 Section", "选择按键后，点击重新加载触发配置`n ")
    MyGui.Add("Text", "xs")
    MyGui.Add("Text", "ym+50 xm+0 Section", "卡刀按键类型：")
    global b1Radio:=MyGui.Add("Radio","ym+50 x+2 vb1Radio", "侧键1")
    global b2Radio:=MyGui.Add("Radio","ym+50 x+2 vb2Radio", "侧键2")
    if(obj.keyModel=="0") {
        b1Radio.value:=1
        b2Radio.value:=0
        Hotkey "XButton1",ssCode
    } else if(obj.keyModel=="1") {
        b1Radio.value:=0
        b2Radio.value:=1
    }
    MyGui.Add("Text", "xs")

    MyGui.Add("Text",, "宏定义")
    MyGui.Add("Text","y+5 xm+14 Section", "R:") ; 保存此控件位置并定义一个新控件段.
    rEdit := MyGui.Add("Edit", "x+30 w50")
    rEdit.value:=obj.rKey

    MyGui.Add("Text", "y+5 xm+15", "T:")
    tEdit := MyGui.Add("Edit", "x+30 w50")
    tEdit.value:=obj.tKey

    MyGui.Add("Text", "y+5 xm+14", "X:")
    MyGui.Add("Text", "x+30", obj.x)
    MyGui.Add("Text", "y+5 xm+14", "Y:")
    MyGui.Add("Text", "x+30", obj.y)
    MyGui.Add("Text", "y+5 xm+0", "取色值:" )
    MyGui.Add("Text", "x+5", obj.color)
    MyGui.Add("Text", "xs")
    MyBtn := MyGui.AddButton("w80", "重新加载")
    MyBtn.OnEvent("Click", loadAhk) ; 当点击时, 调用 MyBtn_Click.
    MyGui.Show
}

loadAhk(*) {
    if(b1Radio.value) {
        obj.keyModel:="0"
    } else if(b2Radio.value) {
        obj.keyModel:="1"
    }
    obj.rKey:=rEdit.value
    obj.tKey:=tEdit.value
    InIWriter()
    Reload
}

Hotkey codeKey, code

code(ThisHotkey)
{
    While GetKeyState(codeKey,"P") {
        SendInput "{r}"
        color := PixelGetColor(obj.x, obj.y)
        Sleep obj.rKey
        if(obj.color!=color) {
            SendInput "{t}"
            Sleep obj.tKey
        }
    }
}

ssCode(ThisHotkey) {
    SendInput "{s}"
    Sleep 50
    SendInput "{s}"
}
